<?php

class ModelCatalogFilter extends Model
{
    public function addFilter($datas, $product_id = null, $category_id = null)
    {
        $language_id = 1;
        foreach ($datas as $k => $data) {

            if (!empty($data['group_name'])) {
                $sql = "select * from " . DB_PREFIX . "filter_group_description where  language_id=$language_id and `name` = '" . $this->db->escape($data['group_name']) . "' limit 1";
                $query = $this->db->query($sql);
                $group = $query->row;
                if ($group) {
                    $filter_group_id = $group['filter_group_id'];
                } else {
                    $this->db->query("INSERT INTO `" . DB_PREFIX . "filter_group` SET sort_order = '999'");
                    $filter_group_id = $this->db->getLastId();
                    $this->db->query("INSERT INTO " . DB_PREFIX . "filter_group_description SET filter_group_id = '" . (int)$filter_group_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($data['group_name']) . "'");
                }
            }

            if (!empty($data['filter_name']) && $filter_group_id) {
                $sql = "select * from " . DB_PREFIX . "filter_description where language_id=$language_id and filter_group_id = ".(int)$filter_group_id." and `name` = '" . $this->db->escape($data['filter_name']) . "'";
                $query = $this->db->query($sql);
                $current_filter = $query->row;
                if ($current_filter) {
                    $filter_id = $current_filter['filter_id'];
                } else {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "filter SET filter_group_id = '" . (int)$filter_group_id . "', sort_order = 999");
                    $filter_id = $this->db->getLastId();
                    $this->db->query("REPLACE INTO " . DB_PREFIX . "filter_description SET filter_id=$filter_id,language_id = '" . (int)$language_id . "', filter_group_id = '" . (int)$filter_group_id . "', `name` = '" . $this->db->escape($data['filter_name']) . "'");
                }
                if (!empty($product_id)) {
                    $sql = "REPLACE INTO " . DB_PREFIX . "product_filter set product_id=" . $product_id . ",filter_id=$filter_id";
                    $this->db->query($sql);
                }
                if (!empty($category_id)) {
                    if (is_array($category_id)) {
                        foreach ($category_id as $cid) {
                            $sql = "REPLACE INTO " . DB_PREFIX . "category_filter set category_id=" . $cid . ",filter_id=$filter_id";
                            $this->db->query($sql);
                        }
                    } else if (is_numeric($category_id)) {
                        $sql = "REPLACE INTO " . DB_PREFIX . "category_filter set category_id=" . $category_id . ",filter_id=$filter_id";
                        $this->db->query($sql);
                    }
                }
            }
        }
    }
}
